package com.cn;

import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;


/**
 * 老师流程审批
 * 用户和用户组关系维护
 */
@SpringBootTest
public class teacher_approver02 {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private HistoryService historyService;


    /**
     * 导入流程图老师流程
     */
    @Test
    void startTeacherApprover(){
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process01-基础/老师审批流程.bpmn20.xml")
                .name("teackerApprover02")
                .deploy();
        System.out.println(deploy.getId());
    }

    /**
     * 启动流程
     */
    @Test
    void startTask(){
        Group group1 = identityService.createGroupQuery().groupId("group1").singleResult();
        HashMap<String, Object> map = new HashMap<>();
        map.put("group1",group1.getId());
        ProcessInstance processInstance = runtimeService.startProcessInstanceById("teacker_approver:2:e25efdf7-cee6-11ee-a740-30d04231d1ff", map);
        System.out.println(processInstance.getProcessDefinitionId()+"实力id");
        System.out.println(processInstance.getId()+"id");
    }

    /**
     * 查询候选人任务
     */
    @Test
    void queryTask(){
        Group student1 = identityService.createGroupQuery().groupMember("student1").singleResult();

        String processInstanceId ="88bda0b4-cee7-11ee-9d26-30d04231d1ff";
        List<Task> list = taskService.createTaskQuery()
                .taskId(processInstanceId)
                .taskCandidateGroup(student1.getId())
                .list();
        for (Task task : list) {
            System.out.println(task.getId()+"id");
            System.out.println(task.getName()+"名称");
        }
    }


}
